import os

def count_labels_in_file(filepath):
    """统计文件中每个类的标签数量"""
    counts = {}
    with open(filepath, 'r') as file:
        for line in file:
            # YOLO格式的标签：class x_center y_center width height
            class_id = int(line.split()[0])
            counts[class_id] = counts.get(class_id, 0) + 1
    return counts

def main(directory):
    overall_counts = {}

    # 遍历指定目录中的所有.txt文件
    for filename in os.listdir(directory):
        if filename.endswith(".txt"):
            filepath = os.path.join(directory, filename)
            file_counts = count_labels_in_file(filepath)
            for class_id, count in file_counts.items():
                overall_counts[class_id] = overall_counts.get(class_id, 0) + count

    # 输出统计结果
    for class_id, count in overall_counts.items():
        print(f"Class {class_id}: {count} labels")

if __name__ == "__main__":
    directory = "F:\Dataset\sumdata(yes)\SumData\labels/val"
    main(directory)
